package com.hainatong.shiphelper.mainhome.adapter;

import androidx.core.content.ContextCompat;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ItemTimelineBinding;
import com.hainatong.shiphelper.mainhome.entity.TimeLineModel;
import com.hainatong.shiphelper.widget.timelineview.TimelineView;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class TimeLineAdapter extends BaseQuickAdapter<TimeLineModel , BaseDataBindingHolder<ItemTimelineBinding>>
{
    public static TimeLineAdapter newInstance(String[] dateTimes)
    {
        List<TimeLineModel> modelList = new ArrayList<>();
        if( dateTimes!= null && dateTimes.length >0 )
        {
            for(int i = 0 ; i < dateTimes.length ;i++   )
            {
                TimeLineModel model = new TimeLineModel();
                model.setStatus(TimeLineModel.StepStatus.ACTIVE );
                model.setDate( dateTimes[i] );
                modelList.add( model );
            }
        }
        TimeLineAdapter adapter = new TimeLineAdapter();
        adapter.setList(modelList);
        return adapter;
    }


    public void setDateTimes(String[] dateTimes )
    {
        List<TimeLineModel> modelList = new ArrayList<>();
        if( dateTimes!= null && dateTimes.length >0 )
        {
            for(int i = 0 ; i < dateTimes.length ;i++   )
            {
                TimeLineModel model = new TimeLineModel();
                model.setStatus(TimeLineModel.StepStatus.ACTIVE );
                model.setDate( dateTimes[i] );
                modelList.add( model );
            }
        }
        setList( modelList );
    }

    public void  addDateTimes(String[] dateTimes)
    {
        List<TimeLineModel> modelList = new ArrayList<>();
        if( dateTimes!= null && dateTimes.length >0 )
        {
            for(int i = 0 ; i < dateTimes.length ;i++   )
            {
                TimeLineModel model = new TimeLineModel();
                model.setStatus(TimeLineModel.StepStatus.ACTIVE );
                model.setDate( dateTimes[i] );
                modelList.add( model );
            }
        }
        addData( modelList );
    }

    private TimeLineAdapter()
    {
        super(R.layout.item_timeline);
    }

    @Override
    protected void convert(@NotNull BaseDataBindingHolder<ItemTimelineBinding> bindingHolder, TimeLineModel timeLineModel)
    {

        ItemTimelineBinding binding = bindingHolder.getDataBinding();
        if( binding != null )
        {
            binding.textTimelineDate.setText( timeLineModel.getDate() );
            binding.timeline.setLineStyle(TimelineView.LineStyle.NORMAL);
            binding.timeline.setStartLineColor(ContextCompat.getColor(getContext(),R.color.gray_point) , TimelineView.getTimeLineViewType( getItemPosition(timeLineModel) , getData().size() ));
            binding.timeline.setEndLineColor(ContextCompat.getColor(getContext(),R.color.gray_point),TimelineView.getTimeLineViewType(getItemPosition(timeLineModel) , getData().size()) );
        }
    }
}
